package com.dissertation.action;

import com.dissertation.pojo.Message;
import com.dissertation.service.IStatisticsByUser;
import com.dissertation.service.IStatisticsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Optional;

@RestController
@RequestMapping( "statistics" )
public class StatisticsController {

    @Resource( name = "indexStatistics" )
    IStatisticsService statisticsService;

    /**
     * 这里可能存在bug，因为权限涉及到二级目录
     *
     * @param categoryTreeName 一级目录名
     * @return
     */
    @GetMapping( "/categoryTree/count" )
    Message getCountByCategoryTreeName( String categoryTreeName ) {
        return Message.createSuc(
                statisticsService.groupByCategoryTree( categoryTreeName )
        );
    }

    /**
     * @param categoryTreeName 不能为null
     * @return
     */
    @GetMapping( "/categoryTree/year" )
    Message getCountMonth( Optional<String> categoryTreeName ) {

        return Message.createSuc( categoryTreeName.map(
                s -> statisticsService.getCountMonth( s ) )
                .orElse( Collections.emptyList() ) );
    }


    @Resource( name = "userStatisticsGroup" )
    IStatisticsByUser statisticsByUser;


    /**
     * 获取一级目录下所有用户对每个资源的结题信息
     *
     * @param categoryTreeName
     * @param type 0 是所有人 1 是 作者 2 是 参与者
     * @return
     */
    @GetMapping( "/user/count" )
    Message getAllUserCount( String categoryTreeName,
                             @RequestParam( defaultValue = "0" ) int type ) {
        if ( categoryTreeName == null ) {
            return Message.createErr( "一级目录名不能为空！" );
        }

        if ( type == 0 ) {
            return Message.createSuc( statisticsByUser.getAllUserCount( categoryTreeName ) );
        } else if ( type == 1 ) {
            return Message.createSuc( statisticsByUser.getAuthorUserCount( categoryTreeName ) );
        } else if ( type == 2 ) {
            return Message.createSuc( statisticsByUser.getParticipantUserCount( categoryTreeName ) );
        } else return Message.createErr( "对不起，type不规范" );
    }
}
